import Vue from 'vue'
import Vuex from 'vuex'
import { userLogin } from '@/api/user'
import { Toast } from 'vant'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    isLogin: localStorage.getItem('isLogin') === 'true',
    aa: 'aa',
    bb: 11
  },
  mutations: {
    changeIsLogin (state, value) {
      state.isLogin = value
    },
    changeAA (state, value) {
      state.aa = value
    }
  },
  actions: {
    async loginAction (context, params) {
      userLogin(params).then(res => {
        if (res.code === '10010') {
          Toast('账号密码错误')
        } else {
          localStorage.setItem('isLogin', true)
          localStorage.setItem('token', res.data.token)
          localStorage.setItem('userid', res.data.userid)
          context.commit('changeIsLogin', true)
        }
      })
    }
  },
  modules: {
  }
})
